<?php
/**
 * Created by PhpStorm.
 * User: 郑合达
 * Date: 2018/12/6
 * Time: 17:02
 */

namespace app\admin\controller;
use app\Model\ArticleModel;
use app\Model\ArticleCatModel;
class Article extends Common
{
    /**
     * @var int
     * 文章列表页面
     * 返回列表数据
     */
    private $page = 20;
    public function article_list(){
        session('urls', null);
        $info['list'] = ArticleModel::alias('a')
            ->field('a.title,a.article_id,a.add_time,a.is_open,a.cat_id,a.thumb')
            ->join('tp_article_cat ac','ac.cat_id = a.cat_id')
            ->field('ac.cat_name,ac.keywords')
            ->order('a.article_id desc')
            ->paginate($this->page);
        $info['category']=$this->article_type();
        $this->getConAndAct('content');
        return view('index',$info);
    }

    /**
     * @return \think\response\View
     * 搜索文章
     */
    public function search(){
        $consignee = input('consignee');
        $shipping_status = input('shipping_status');
        $where = [];
        if($consignee!=0){
            $where['a.title']  = ['like','%'.$consignee.'%'];
        }
        if ($shipping_status){
            $where['a.cat_id']  = $shipping_status;
        }
        $info['list'] = ArticleModel::alias('a')
            ->field('a.title,a.article_id,a.add_time,a.is_open,a.cat_id,a.thumb')
            ->join('tp_article_cat ac','ac.cat_id = a.cat_id')
            ->field('ac.cat_name,ac.keywords')
            ->where($where)
            ->order('a.article_id desc')
            ->paginate($this->page);
        $info['category']=$this->article_type();
        return view('index',$info);
    }

    /**
     * @return \think\response\Json
     * 根据article_id 更改显示状态
     */
    public function change(){
       $article_id =input('article_id');
        $is_open =input('is_open');
       $info  = ArticleModel::update([
           'is_open' => $is_open
       ], ['article_id' => $article_id]);
        if(!$info){
            return retJson('', 201, '修改失败');
        }
        return retJson('', 200, '修改成功');
    }

    /**
     * @return \think\response\View
     * 新增文章页面
     */
    public function article_add(){
//        $info['category']= article_cat_list(0,$this->article_type());
        $info['category']=$this->article_type();
        $this->assign('info',$info);
        return view('add',$info);
    }

    /**
     * @return \think\response\Json
     * 新增文章
     */
    public function add(){
        //数据验证
       $this->veri('article','add');
        $ret = ArticleModel::create([
            'cat_id'        => input('cat_id'),
            'title'         => input('title'),
            'content'       =>  input('content'),
            'keywords'      => input('keywords'),
            'link'          => input('link'),
            'is_open'       =>input('is_open'),
            'publish_time' =>strtotime(input('publish_time')),
            'description'       =>input('description'),
            'thumb'       =>input('thumb')
        ]);
        if(!$ret){
            return retJson('', 201, '新增失败');
        }

        return retJson('', 200, '新增成功');
    }
    /**
     * @return \think\response\Json
     * 删除文章
     */
    public function del(){
        if (input('thumb')){
            if (file_exists("./upload/article/".input('thumb'))){
                unlink("./upload/article/".input('thumb'));//删除图片
            }
        }
        $del = ArticleModel::destroy(input('id'));
        if(!$del){
            return retJson('', 201, '删除失败！');
        }
        return retJson('', 200, '删除成功！');
    }

    /**
     * @return \think\response\View
     * 跳转编辑页面，返回编辑参数
     */
    public function article_edit(){
        $data= ArticleModel::field('*')->where('article_id',input('article_id'))
            ->find();
        $info['category']=$this->article_type();
        $this->assign('data',$data);
        return view('edit',$info);
    }

    /**
     * @return \think\response\Json
     * 修改文章
     */
    public function edit(){
        //数据验证
        $this->veri('article','edit');
        //判断是否上传图片
        if (input('thumb')!=1){
            //判断图片是否存在
            if (input('img')){
                unlink("./upload/article/".input('img'));//删除图片
            }
            $img=input('thumb');
        }else{
            $img=input('img');
        }

        $data['cat_id']   = input('cat_id');
        $data['title']   = input('title');
        $data['content']= input('content');
        $data['keywords'] = input('keywords');
        $data['link']   = input('link');
        $data['is_open']   = input('is_open');
        $data['publish_time']   = strtotime(input('publish_time'));
        $data['description']   = input('description');
        $data['thumb']   = $img;
        // 文章信息修改
        $res = ArticleModel::update($data, ['article_id' => input('article_id')]);
        if(!$res){
            return retJson('', 201, '修改失败');
        }

        return retJson('', 200, '修改成功');
    }



    /**
     * @return false|\PDOStatement|string|\think\Collection
     * 返回文章类别数据
     */
    public function article_type(){
        $info['category'] = ArticleCatModel::field('cat_id,cat_name')->order('sort_order')->select();
        return  $info['category'];
    }

    //图片上传
    public function webuploader(){
        if (session('urls')){
            unlink("./".session('urls'));//删除替换的图片
        }
        $file = $this->request->file('file');//file是传文件的名称
        $info = $file->move(ROOT_PATH . 'public/' . DS . 'upload/article');
        $names=str_replace("\\","/",$info->getSaveName());//把反斜杠(\)替换成斜杠(/)
        $temp_path='/upload/article/';
        session('urls',$temp_path.$names);
        $arr = array(
            'link' =>$temp_path.$names,
            'filename' => $names,
        );
        return json($arr);
    }
}